package UMC.Data.Sql;

import UMC.Data.Utility;

public class OracleDbProvider extends DbProvider {
    static {
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String quotePrefix() {
        return "\"";
    }

    @Override
    public String quoteSuffix() {
        return "\"";
    }

    @Override
    public String paginationText(int start, int limit, String selectText) {
        return String.format("SELECT * FROM (SELECT A.*, ROWNUM R__ FROM (%s) A WHERE ROWNUM <= %d)WHERE R__ > %d ", selectText, start + limit, start);

    }

    @Override
    public String conntionString() {
        String port = this.provider.get("port");
        if (Utility.isEmpty(port)) {
            port = "1521";
        }

        return String.format("jdbc:oracle:thin@%s:%s:%s", provider.get("host"), port, provider.get("database"));//, provider.get("host"), port, provider.get("database"));
//        return null;
    }

    @Override
    public DbBuilder builder() {
        return new OracleBuilder();
    }

    class OracleBuilder extends DbBuilder {
        @Override
        public String AddColumn(String name, String field, String type) {
            return String.format("ALTER TABLE %s ADD(%s %s)", name, field, type);
        }

        @Override
        public String Binary() {
            return "BLOB";
        }

        @Override
        public String Boolean() {
            return "NUMBER";
        }

        @Override
        public String Date() {
            return "DATE";
        }

        @Override
        public String DropColumn(String name, String field) {
            return String.format("ALTER TABLE %s DROP COLUMN %s", name, field);
        }

        @Override
        public String Float() {
            return "FLOAT";
        }

        @Override
        public String Guid() {
            return "CHAR(36)";
        }

        @Override

        public String Integer() {
            return "NUMBER";
        }

        @Override
        public String Column(String field) {
            return field.toUpperCase();
        }

        @Override
        public String Number() {
            return "NUMBER(16,2)";
        }

        @Override
        public String PrimaryKey(String name, String... fields) {
            StringBuilder sb = new StringBuilder();
            sb.append("ALTER TABLE ");
            sb.append(name);
            sb.append(" ADD PRIMARY KEY (");// id);")
            for (int i = 0; i < fields.length; i++) {

                sb.append(fields[i]);//"{0}", s);
                sb.append(',');

            }
            sb.deleteCharAt(sb.length() - 1);
            sb.append(")");
            return sb.toString();
        }

        @Override
        public String String() {
            return "NVARCHAR2(500)";
        }

        @Override
        public String Text() {
            return "VARCHAR2(4000)";
        }

        @Override
        public Boolean Check(String name, String field, ISqler sqler) {

            return ((Integer) sqler.executeScalar("SELECT COUNT(*) FROM USER_TAB_COLUMNS WHERE TABLE_NAME = {0} AND COLUMN_NAME = {1}", name.toUpperCase(), field)) > 0;
        }

        @Override
        public Boolean Check(String name, ISqler sqler) {
            return ((Integer) sqler.executeScalar("SELECT COUNT(*) FROM USER_TAB_COLUMNS WHERE TABLE_NAME = {0}", name.toUpperCase())) > 0;
        }
    }
}
